package com.heu.blood.transfusion.controller;

import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.common.utils.PdfUtil;
import com.heu.blood.transfusion.entity.TransfusionApplicationEntity;
import com.heu.blood.transfusion.service.TransfusionApplicationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@Slf4j
@RestController
@RequestMapping("/example")
@Api(tags = "PDF操作接口")
public class TestPdfController {
    @Autowired
    private TransfusionApplicationService transfusionApplicationService;


    @ApiOperation(value = "根据PDF模板导出PDF")
    @GetMapping("/exportPdf")
    @ControllerLogAnnotation(module = "PDF操作", operate = "根据PDF模板导出PDF")
    public void exportPdf(HttpServletResponse response,TransfusionApplicationEntity transfusionApplicationEntity) {
        Map<String, Object> dataMap = new HashMap<>(16);
        String transfusionId = transfusionApplicationEntity.getTransfusionId();
        //String transfusionId = "1";
        TransfusionApplicationEntity transfusionApplication = transfusionApplicationService.queryByTransfusionId(transfusionId);
        dataMap.put("patientId",transfusionApplication.getPatientId());
        dataMap.put("transfusionId", transfusionApplication.getTransfusionId());
        dataMap.put("visitNumber", transfusionApplication.getVisitNumber());
        dataMap.put("idcard", transfusionApplication.getIdcard());
        dataMap.put("name", transfusionApplication.getName());
        dataMap.put("gender", transfusionApplication.getGender());
        dataMap.put("age", transfusionApplication.getAge());
        dataMap.put("national",transfusionApplication.getNational());
        dataMap.put("department", transfusionApplication.getDepartment());
        dataMap.put("ward",transfusionApplication.getWard() );
        dataMap.put("bedNumber", transfusionApplication.getBedNumber());
        dataMap.put("clinicalDiagnosis",transfusionApplication.getClinicalDiagnosis() );
        dataMap.put("pregnancy", transfusionApplication.getPregnancy());
        dataMap.put("maternity", transfusionApplication.getMaternity());

        System.out.println(dataMap);

        Map<String, String> photoMap = new HashMap<>(16);
       // photoMap.put("logo", "https://profile.csdnimg.cn/C/9/4/2_qq_34988304");

        // 设置response参数，可以打开下载页面
        response.reset();
        response.setCharacterEncoding("UTF-8");
        // 定义输出类型
        response.setContentType("application/PDF;charset=utf-8");
        // 设置名称
        response.setHeader("Content-Disposition", "attachment; filename=" + "demo.pdf");
        try (ServletOutputStream out = response.getOutputStream()) {
            // 模板路径记
            PdfUtil.fillTemplate(dataMap, photoMap, out, "src/main/resources/templates/demo.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//
}
